package com.fx.attendance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fx.annotation.Log;
import com.fx.attendance.ai.AiFaceTemplate;
import com.fx.attendance.domain.FaceInformation;
import com.fx.attendance.dto.FaceInformationQueryCriteria;
import com.fx.attendance.exception.ApiException;
import com.fx.attendance.service.FaceInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: lijie
 * @date 2020-04-18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/faceInformation")
@Api(tags = "人脸表服务接口")
        public class FaceInformationController{

        private final FaceInformationService faceInformationService;

        private final AiFaceTemplate aiFaceTemplate;

        @Log("导出数据")
        @ApiOperation("导出数据")
        @GetMapping(value = "/download")
        public void download(HttpServletResponse response, FaceInformationQueryCriteria criteria) throws IOException {
                faceInformationService.download(faceInformationService.queryAll(criteria), response);
        }

        @GetMapping
        @Log("查询用户画像")
        @ApiOperation("查询用户画像")
        public ResponseEntity<Object> query(FaceInformationQueryCriteria criteria, Pageable pageable){
                return new ResponseEntity<>(faceInformationService.queryAll(criteria,pageable),HttpStatus.OK);
        }

        @PostMapping
        @Log("新增用户画像")
        @ApiOperation("新增用户画像")
        @PreAuthorize("@el.check('faceInformation:add')")
        public ResponseEntity<Object> create(@Validated @RequestBody FaceInformation resources){
                return new ResponseEntity<>(faceInformationService.create(resources),HttpStatus.CREATED);
        }

        @PutMapping
        @Log("修改用户画像")
        @ApiOperation("修改用户画像")
        public ResponseEntity<Object> update(@Validated @RequestBody FaceInformation resources){
                faceInformationService.update(resources);
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        @Log("删除用户画像")
        @ApiOperation("删除用户画像")
        @DeleteMapping
        public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
                faceInformationService.deleteAll(ids);
                return new ResponseEntity<>(HttpStatus.OK);
        }

        @ApiOperation("新增人脸")
        @PostMapping("/addFaceToDataBase")
        public ResponseEntity<Object> addFace(@RequestBody String param) throws Exception {
                JSONObject jsonObject= JSON.parseObject(param).getJSONObject("data");
                faceInformationService.addFace(jsonObject);
                return new ResponseEntity<>(HttpStatus.OK);
        }

        @PostMapping("/compareWithTwoImh")
        @ApiOperation("对比两张人脸，开始考勤")
        public String compareWithTwoImh(MultipartFile file1, MultipartFile file2) throws Exception{
                String[] res= AiFaceTemplate.stream2Base64(file1,file2);
                return aiFaceTemplate.compareWithTwoImh(res[0],res[1]);
        }

        @GetMapping("/getALlFaces")
        @ApiOperation("获取所有人脸及用户信息")
        public ResponseEntity<Object> getALlFaces() throws Exception{
                return new ResponseEntity<>(faceInformationService.getALlFaces(),HttpStatus.OK);
        }




 }

